package com.huangmb.anymark.app.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import com.huangmb.anymark.app.R;

/**
 * @author hmb
 * @version 1.0
 * @since 2016/4/19 21:54
 */
public class SuperRecyclerView extends android.support.v7.widget.RecyclerView {
    public final static int LAYOUT_MANAGER_LINEAR = 0;
    public final static int LAYOUT_MANAGER_GRID = 1;
    public final static int LAYOUT_MANAGER_STAGGERED = 2;
    private Adapter mAdapter;
    private AdapterDataObserver mDataObserver;
    private View emptyView;

    public SuperRecyclerView(Context context) {
        this(context, null);
    }

    public SuperRecyclerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SuperRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SuperRecyclerView, defStyle, 0);


            int layoutManagerType = a.getInt(R.styleable.SuperRecyclerView_layoutManagerType, LAYOUT_MANAGER_LINEAR);
            LayoutManager layoutManager = null;
            switch (layoutManagerType) {
                case LAYOUT_MANAGER_LINEAR:
                    layoutManager = new LinearLayoutManager(context, attrs, defStyle, 0);
                    break;
                case LAYOUT_MANAGER_STAGGERED:
                    layoutManager = new StaggeredGridLayoutManager(context, attrs, defStyle, 0);
                    break;
                case LAYOUT_MANAGER_GRID:
                    layoutManager = new GridLayoutManager(context, attrs, defStyle, 0);
                    break;
                default:
                    break;
            }
            if (layoutManager != null) {
                setLayoutManager(layoutManager);
            }
            int emptyViewId = a.getResourceId(R.styleable.SuperRecyclerView_emptyView, -1);
            if (emptyViewId != -1) {
                emptyView = LayoutInflater.from(context).inflate(emptyViewId, this, false);
                setEmptyView(emptyView);
            }
            a.recycle();
        }
    }

    @Override
    public void setAdapter(Adapter adapter) {
        super.setAdapter(adapter);
        if (mAdapter != null && mDataObserver != null) {
            mAdapter.unregisterAdapterDataObserver(mDataObserver);
        }
        mAdapter = adapter;
        if (mAdapter != null) {
            mDataObserver = new AdapterDataObserver() {
                @Override
                public void onChanged() {
                    if (emptyView != null) {
                        if (mAdapter.getItemCount() == 0) {
                            emptyView.setVisibility(VISIBLE);
                        } else {
                            emptyView.setVisibility(GONE);
                        }
                    }
                }
            };
            mAdapter.registerAdapterDataObserver(mDataObserver);
        }
    }

    public void setEmptyView(View view) {
        emptyView = view;
        if (emptyView != null) {
            int visible = getAdapter() == null || getAdapter().getItemCount() == 0 ? VISIBLE : GONE;
            emptyView.setVisibility(visible);
        }

    }
}
